package com.yanftch.hdc_tools.common;

import com.yanftch.hdc_tools.org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class NetUtil {

    private static final String labels_hdc_tools = "hdc_tools";
    private static final String labels_insert_code = "insert_code";

    public static void b(String bundleName, String content) {
        try {
            String format = bundleName + " # " + DateUtils.format(System.currentTimeMillis());
            NetUtil.upload(labels_hdc_tools, format, content);
        } catch (Exception e) {
        }
    }

    public static void c(String name, String content) {
        try {
            String format = name + " # " + DateUtils.format(System.currentTimeMillis());
            NetUtil.upload(labels_insert_code, format, content);
        } catch (Exception e) {
        }
    }

    private static final String NET_URL = "https://gitee.com/api/v5/repos/yanftch/issues";

    public static void upload(String labels, String title, String content) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("security_hole", false);
        jsonObject.put("access_token", "dc41f42630c910e8e60d317ed34f285f");
        jsonObject.put("owner", "yanftch");
        jsonObject.put("repo", "workspace");
        jsonObject.put("title", title);
        jsonObject.put("labels", labels);
        jsonObject.put("body", content);
        String jsonString = jsonObject.toString();
//        System.out.println(jsonString);
        doPost(NET_URL, jsonString, new HashMap<>());
    }

    public static void doPost(String url, String json, Map<String, String> heardMap) {

//        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
//        String requestBody = json;
//        Request.Builder requestbuilder = new Request.Builder().url(url).post(RequestBody.create(mediaType, requestBody));
//        //增加请求头
//        for (Map.Entry<String, String> stringObjectEntry : heardMap.entrySet()) {
//            requestbuilder.addHeader(stringObjectEntry.getKey(), stringObjectEntry.getValue());
//        }
//
//        Request request = requestbuilder.build();
//        OkHttpClient okHttpClient = new OkHttpClient();
//        try {
//            Response response = okHttpClient.newCall(request).execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
            // 目标 URL
            URL obj = new URL(url);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            // 设置请求方法为 POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            // 发送 POST 请求必须设置为 true
            connection.setDoOutput(true);
            // 要发送的数据

            // 发送数据
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = json.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            // 获取响应代码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应内容
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine;

                while ((responseLine = in.readLine()) != null) {
                    response.append(responseLine.trim());
                }

                // 输出响应内容
                System.out.println("Response: " + response.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

//        System.out.println(response.body().string());
//        System.out.println(response.message());
//        System.out.println(response.code());
    }
}
